package model;

import state.FarmMachineContext;
import state.IdleState;
import state.MaintenanceState;
import state.WorkingState;

public class Harvester implements FarmMachine {
    private final String deviceId;
    private FarmMachineContext stateContext;
    private boolean isRunning = false;

    public Harvester(String deviceId) {
        this.deviceId = deviceId;
        this.stateContext = new FarmMachineContext();
    }

    // 实现 AgriculturalDevice 方法
    @Override
    public void start() {
        if (stateContext.getState() instanceof IdleState) {
            if (!isRunning) {
                isRunning = true;
                System.out.println("Harvester " + deviceId + " 启动");
                stateContext.request(); // 触发状态转换
            }
        } else {
            System.out.println("Harvester " + deviceId + " 不在待机状态，无法启动");
        }
    }

    @Override
    public void stop() {
        if (isRunning) {
            isRunning = false;
            System.out.println("Harvester " + deviceId + " 停止");
            stateContext.setState(new IdleState());
        }
    }

    @Override
    public void emergencyStop() {
        isRunning = false;
        System.out.println("Harvester " + deviceId + " 紧急停止");
    }

    @Override
    public String getCurrentParams() {
        return "Harvester " + deviceId + " 参数: " + (isRunning ? "运行中" : "已停止");
    }

    @Override
    public String getDeviceId() {
        return deviceId;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    // 实现 FarmMachine 方法
    @Override
    public void performTask() {
        if (isRunning) {
            System.out.println("Harvester " + deviceId + " 执行收割任务");
        } else {
            System.out.println("错误：Harvester " + deviceId + " 未启动，无法执行任务");
        }
    }

    // 实现 EnvironmentObserver 方法
    @Override

    public void update(String sensorType, double value) {
        System.out.println("Harvester " + deviceId + " 接收到传感器更新: " + sensorType + " = " + value);
        // 根据传感器数据调整设备参数
        if (sensorType.equals("土壤湿度") && value > 80) {
            adjust("speed=slow");
            System.out.println("避障");
        }
        else if (sensorType.equals("路障大小")) {
            if (value > 50) {
                System.out.println("避障");
            }
        }
    }



    @Override
    public void adjust(String params) {
        System.out.println("Harvester " + deviceId + " 调整参数: " + params);
        // 实际调整逻辑
    }
    @Override
    public String getName() {
        return "收割机";
    }

    @Override
    public String getType() {
        return "HARVESTER";
    }

    @Override
    public FarmMachineContext getStateContext() {
        return stateContext;
    }

    @Override
    public void setStateContext(FarmMachineContext stateContext) {
        this.stateContext = stateContext;
    }

    @Override
    public String getStatus() {
        return isRunning() ? "运行中" : "停止";
    }

    @Override
    public String getStatusDescription() {
        if (stateContext.getState() instanceof IdleState) {
            return "待机";
        } else if (stateContext.getState() instanceof WorkingState) {
            return "运行中";
        } else if (stateContext.getState() instanceof MaintenanceState) {
            return "维护中";
        }
        return "未知状态";
    }
}
